﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using System.Threading;
public class PlayThread //: Thread
{
    private readonly int key;
    //private readonly CountdownEvent stop;
    private readonly bool nextPause;
    private readonly float volume;
    AudioSource asource;
    MonoBehaviour monoBehaviour;

    public PlayThread(int key, bool nextPause, bool silent)
    {
        if (silent)
            volume = 0f;
        else
            volume = 1f;

        this.nextPause = nextPause;
        this.key = key;
        //this.stop = new CountdownEvent(1);

        // Log.d("Piano", "PlayThread: " + key);
    }
    public void Start(MonoBehaviour mono)
    {
        monoBehaviour = mono;
        FM_SoundPool.soundMap.TryGetValue(key, out AudioClip clip);
        //Debug.LogError($"Run key:{key} clip:{clip!= null}");
        asource = AudioController.instance.Play(clip);

       // mono.StartCoroutine(Run());
    }

    public void Stop()
    {
        //waitTime = 0;
        //stop.Signal();

        //FM_SoundPool.soundMap.TryGetValue(key, out AudioClip clip);
        //AudioController.instance.Stop(clip);
        if (asource)
        {
            monoBehaviour.StartCoroutine(IEStop(asource));
        }
        asource = null;
        monoBehaviour = null;
    }

    float waitTime = 8;
    public  IEnumerator Run()
    {
        //int stream = sndPool.Play(soundMap.Get(key), volume, volume, 100, 0, 1);
        FM_SoundPool.soundMap.TryGetValue(key, out AudioClip clip);
        //Debug.LogError($"Run key:{key} clip:{clip!= null}");
        AudioSource asource = AudioController.instance.Play(clip);

        //waitTime = 8;
        //yield return new WaitForSeconds(8);
        //float timer = 0;
        //while(timer < waitTime)
        //{
        //    timer += Time.deltaTime;
        //    yield return 0;
        //}

        if (volume > 0)
        {
            // start fallback sequence
            float d = 0f;
            float fall = FM_SoundPool.FALLBACK_DURATION;
            if (nextPause)
                fall = fall / 5f;

            fall /= 250f;
            while (d < fall)
            {
                d += Time.deltaTime;
                float p = 1f - d / fall;
                asource.volume = p;

                //Debug.LogError($"d:{d}  fall:{fall}");
                yield return null;
            }
            asource.volume = 0;
        }
        AudioController.instance.Stop(clip);
    }

    public IEnumerator IEStop(AudioSource asource)
    {
        if (volume > 0)
        {
            // start fallback sequence
            float d = 0f;
            float fall = .2f;
            while (d < fall)
            {
                d += Time.deltaTime;
                float p = 1f - d / fall;
                asource.volume = p;

                //Debug.LogError($"d:{d}  fall:{fall}");
                yield return null;
            }
            asource.volume = 0;
        }
        AudioController.instance.Stop(asource.clip);
    }
}
